package com.zky.pet.service.impl;

import com.zky.basic.dto.MsgDto;
import com.zky.basic.exception.BusinessException;
import com.zky.basic.service.IBaiduAiAuditService;
import com.zky.order.domain.OrderPetAcquisition;
import com.zky.order.mapper.OrderPetAcquisitionMapper;
import com.zky.org.domain.Employee;
import com.zky.org.domain.Shop;
import com.zky.org.mapper.EmployeeMapper;
import com.zky.org.mapper.ShopMapper;
import com.zky.pet.domain.SearchMasterMsg;
import com.zky.pet.domain.SearchMasterMsgAuditLog;
import com.zky.pet.mapper.SearchMasterMsgAuditLogMapper;
import com.zky.pet.mapper.SearchMasterMsgMapper;
import com.zky.pet.query.SearchMasterMsgQuery;
import com.zky.pet.service.ISearchMasterMsgService;
import com.zky.basic.service.impl.BaseServiceImpl;
import com.zky.user.domain.Logininfo;
import com.zky.user.domain.User;
import com.zky.user.mapper.UserMapper;
import com.zky.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;



    @Override
    public AjaxResult publish(SearchMasterMsg msg, HttpServletRequest request) {
        //1.添加到数据库
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userMapper.findByLogininfoId(logininfo.getId());
        msg.setUserId(user.getId());
        searchMasterMsgMapper.add(msg);

        //2.百度ai智能审核
        Boolean boo1 = baiduAiAuditService.textAudit(msg.getName() + msg.getTitle());
        Boolean boo2 = baiduAiAuditService.imageAudit(msg.getResources());
        if (boo1&&boo2){
            //修改状态
            msg.setState(1);
            //3.添加审核日志.
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setMsgId(msg.getId());
            log.setState(1);
            log.setNote("审核成功");

            searchMasterMsgAuditLogMapper.add(log);
            //获取最近店铺
            Point point = DistanceUtils.getPoint(msg.getAddress());
            Shop shop = DistanceUtils.getNearestShop(point, shopMapper.findAll());
            //判断五十公里内是否有店铺
            if (shop!= null){
                msg.setShopId(shop.getId());
                //发送短信告知店铺
                //SmsUtils.sendSms(shop.getTel(),"你有新的订单，位置在"+msg.getAddress() +"请及时处理");
                System.out.println(shop.getTel()+"你有新的订单，位置在"+msg.getAddress() +"请及时处理");
            }else {
                msg.setState(3);
                //发送短信告知店铺
                //SmsUtils.sendSms("你附件暂时没有店铺，请耐心等待");
                System.out.println("你附件暂时没有店铺，请耐心等待");
            }
            searchMasterMsgMapper.update(msg);
            return new AjaxResult();

        }else {//审核失败
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setNote("审核失败");
            log.setMsgId(msg.getId());
            log.setState(0);
            searchMasterMsgAuditLogMapper.add(log);
            searchMasterMsgMapper.delete(msg.getId());
            return AjaxResult.me().setSuccess(false).setMsg("你的寻主信息不合规，请重新输入");
        }

    }

    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userMapper.findByLogininfoId(logininfo.getId());
        query.setUserId(user.getId());
        return super.pageQuery(query);
    }

    @Override
    public PageList<SearchMasterMsg> finish(SearchMasterMsgQuery query, HttpServletRequest request) {
        query.setState(2);
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLongininfoId(logininfo.getId());
        if (employee.getShop_id()!=null){
            query.setShopId(employee.getShop_id());
        }
        return super.pageQuery(query);
    }

    @Override
    public PageList<SearchMasterMsg> toSearchMasterPool(SearchMasterMsgQuery query) {
        query.setState(3);
        return super.pageQuery(query);
    }

    @Override
    public PageList<SearchMasterMsg> toHandle(SearchMasterMsgQuery query, HttpServletRequest request) {
        query.setState(1);
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLongininfoId(logininfo.getId());
        if (employee.getShop_id()!=null){
            query.setShopId(employee.getShop_id());
        }
        return super.pageQuery(query);
    }

    @Override
    public void reject(Long id) {
        searchMasterMsgMapper.reject(id);
    }

    @Override
    public void accept(MsgDto msgDto) {
        //平台将寻主信息分配给店铺
        //店铺接收，指派员工去接宠物
        //判断订单是否存在-存在则抛异常
        OrderPetAcquisition order =  orderPetAcquisitionMapper.findByMsgId(msgDto.getMsgId());
        //订单在处理
        if (order!=null&&order.getState()==0){
            throw new BusinessException("订单在处理，请勿重复接单");
        }
        //生成订单
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(msgDto.getMsgId());
        OrderPetAcquisition newOrder = creatOrder(msg,msgDto);
        orderPetAcquisitionMapper.add(newOrder);

        //告诉处理人在规定时间内去处理
//        Employee employee = employeeMapper.findOne(msgDto.getHandler());
//        SmsUtils.sendSms(employee.getPhone(),"你有新的订单，请在两天内去"+msg.getAddress()+"接回宠物");
        System.out.println("你有新的订单，请在两天内去"+msg.getAddress()+"接回宠物");
    }

    private OrderPetAcquisition creatOrder(SearchMasterMsg msg, MsgDto msgDto) {
        OrderPetAcquisition order = new OrderPetAcquisition();
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(msgDto.getHandler()));
        order.setDigest(msgDto.getNote());
        order.setLastcomfirmtime(new Date(System.currentTimeMillis()+2*24*60*60*1000));
        //order.setState(1);
        order.setPrice(msg.getPrice());
        //order.setPaytype(0);
        //order.setPetId();
        order.setUserId(msg.getUserId());
        order.setShopId(msg.getShopId());
        order.setAddress(msg.getAddress());
        order.setEmpId(msgDto.getHandler());
        order.setSearchMasterMsgId(msg.getId());
        return order;
    }
}
